package henu.graduation_design.onlineshop;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class StartActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SharedPreferences sp = getSharedPreferences("SP",
				MODE_PRIVATE);
		// 从配置文件里查看是否开始 开局动画
		Boolean showAnimationBoolean = sp.getBoolean("IsAnima", false);
		if (!showAnimationBoolean) {
			Intent intent = new Intent(this, TabActivity.class);
			startActivity(intent);
			finish();
			return;
		}
		View view = View.inflate(this, R.layout.activity_start, null);
		setContentView(view);

		AlphaAnimation firstAnimation = new AlphaAnimation(0.3f, 1.0f);
		firstAnimation.setDuration(1500);
		view.setAnimation(firstAnimation);
		firstAnimation.setAnimationListener(new AnimationListener() {
			public void onAnimationStart(Animation animation) {
			}

			public void onAnimationRepeat(Animation animation) {
			}

			public void onAnimationEnd(Animation animation) {
				titleAnimation();
			}
		});
	}

	private void titleAnimation() {
		final ImageView titleImage = (ImageView) findViewById(R.id.start_title);
		AlphaAnimation titleAnimation = new AlphaAnimation(1.0f, 0.0f);
		titleAnimation.setDuration(1500);
		titleImage.setAnimation(titleAnimation);
		titleAnimation.setAnimationListener(new AnimationListener() {
			public void onAnimationStart(Animation animation) {
			}

			public void onAnimationRepeat(Animation animation) {
			}

			public void onAnimationEnd(Animation animation) {
				titleImage.setVisibility(View.INVISIBLE);
				goTo();
			}
		});
	}

	private void goTo() {
		final LinearLayout mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
		final LinearLayout startLayout = (LinearLayout) findViewById(R.id.startLayout);
		final LinearLayout animLayout = (LinearLayout) findViewById(R.id.animLayout);
		final LinearLayout leftLayout = (LinearLayout) findViewById(R.id.leftLayout);
		final LinearLayout rightLayout = (LinearLayout) findViewById(R.id.rightLayout);

		startLayout.setVisibility(View.GONE);
		animLayout.setVisibility(View.VISIBLE);
		mainLayout.setBackgroundResource(R.drawable.ic_start_background);
		Animation leftOutAnimation = AnimationUtils.loadAnimation(this,
				R.anim.translate_left);
		Animation rightOutAnimation = AnimationUtils.loadAnimation(this,
				R.anim.translate_right);
		leftLayout.setAnimation(leftOutAnimation);
		rightLayout.setAnimation(rightOutAnimation);
		leftOutAnimation.setAnimationListener(new AnimationListener() {
			public void onAnimationStart(Animation animation) {
				mainLayout.setBackgroundColor(getResources().getColor(
						R.color.startBgColor));
			}

			public void onAnimationRepeat(Animation animation) {
			}

			public void onAnimationEnd(Animation animation) {
				leftLayout.setVisibility(View.GONE);
				rightLayout.setVisibility(View.GONE);
				Intent intent = new Intent(StartActivity.this,
						TabActivity.class);
				startActivity(intent);
				finish();
				overridePendingTransition(R.anim.zoom_out_enter,
						R.anim.zoom_out_exit);
			}
		});
	}
}
